// pages/order/list/index.js
Page({
  // 页面的初始数据
  data: {
    orderList: [],
    loading:true,
  },
  onLoad(e){
      this.getList()
  },
  onChange(e){
      console.log(e.detail.index)
      var index=e.detail.index
    switch(index){
      case 0:
      this.getList()
      break
      case 1:
        this.data.orderList.length=0
        this.getTypeInfo(1)
      break
      case 2:
        this.data.orderList.length=0
        this.getTypeInfo(2)
      break
    }
     
  },
getTypeInfo(t){
  wx.cloud.callFunction({
    name:"order",
    data:{
      type:"selectByType",
      t:t
    }
  }).then(res=>{
    console.log(res.result.data)
    const objects=res.result.data
    const pids = objects.map(obj => obj.pid);
    console.log(pids)
   pids.map(pid => this.getInfo(pid));
  })
},
  getList(){
      wx.cloud.callFunction({
        name:"order",
        data:{
          type:"select"
        }
      }).then(res=>{
        console.log(res)
        const objects=res.result.data
        const pids = objects.map(obj => obj.pid);
        console.log(pids)
       pids.map(pid => this.getInfo(pid));
      })
  },
  getInfo(pid){
      wx.cloud.callFunction({
        name:"service",
        data:{
          type:"selectById",
          id:pid
        }
      }).then(res=>{
        this.data.orderList.push(res.result.data)
        this.setData({
          orderList:this.data.orderList,
          loading:false
        })
      }
        )
    
  },
})
